package pw.hais.main.main_app.dao;

import com.google.gson.Gson;
import com.squareup.okhttp.Response;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import pw.hais.app.UtilConfig;
import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.main.R;
import pw.hais.main.main_app.app.HaisApplication;
import pw.hais.main.main_app.app.V;
import pw.hais.main.main_app.model.FileModel;
import pw.hais.main.main_app.model.LeadModel;
import pw.hais.main.main_app.model.UserModel;
import pw.hais.utils.L;
import pw.hais.utils.SPUtils;

/**
 * Created by hais1992 on 2015/12/13.
 */
public class LeanDao {
    private static Map<String, String> header;
    private static Gson gson = UtilConfig.GSON;

    static {
        header = new HashMap<>();
        header.put("X-LC-Id", V.Lean.X_LC_Id);
        header.put("X-LC-Key", V.Lean.X_LC_Key);
    }

    /**
     * 获取手机验证码
     */
    public static void getMsmCode(String mobile, OnHttpListener<LeadModel> listener) {
        Map<String, Object> body = new HashMap<>();
        body.put("mobilePhoneNumber", mobile);
        body.put("name", HaisApplication.app.getResources().getString(R.string.app_name));
        try {
            JSONObject jsonObject = new JSONObject(gson.toJson(body));
            Http.post(V.Lean.HTTP_GET_MSM_CODE, header, null, jsonObject, listener);
        }catch (Exception e){e.printStackTrace();}
    }

    /**
     * 注册用户
     */
    public static void doRegUser(UserModel user, OnHttpListener<UserModel> listener) {
        Map<String, Object> body = new HashMap<>();
        body.put("username", user.username);
        body.put("mobilePhoneNumber", user.mobilePhone);
        body.put("password", user.password);
        body.put("smsCode", user.smsCode);
        try {
            JSONObject jsonObject = new JSONObject(gson.toJson(body));
            Http.post(V.Lean.HTTP_Reg_User, header, null, jsonObject, listener);
        }catch (Exception e){e.printStackTrace();}
    }

    /**
     * 用户登录
     */
    public static void doLoginUser(UserModel user, OnHttpListener<UserModel> listener) {
        Map<String, String> params = new HashMap<>();
        params.put("username", user.username);
        params.put("password", user.password);
        Http.get(V.Lean.HTTP_Login_User, header, params, listener);
    }

    /**
     * 删除文件
     */
    public static void deleteFile(String objectId, OnHttpListener<UserModel> listener) {
        //HTTP_DELETE_FILE
        Http.delete(V.Lean.HTTP_DELETE_FILE+"/"+objectId,header,null,null,listener);
    }

    /**
     * 上传用户头像，并修改头像
     */
    public static void updateUserPhoto(String file, String fileName, final OnHttpListener<LeadModel> listener) {
        L.i("头像上传地址：" + new File(file + "/" + fileName).toString());
        String userphoto_update_url = V.Lean.HTTP_UPTATE_User_PHOTO + "/" + HaisApplication.app.user.username + "_userphoto.jpg";
        Http.post(userphoto_update_url, header, null, new File(file + "/" + fileName), new OnHttpListener<FileModel>() {
            @Override
            public void onSuccess(Response response, FileModel data) {
                data.__type = "File";
                HaisApplication.app.user.userPhoto = data;
                UserModel user = HaisApplication.app.user;
                SPUtils.saveObject(V.SP.USER,user);
                Map<String, String> header_copy = header;
                header_copy.put("X-LC-Session", user.sessionToken);

                Map<String, Object> body = new HashMap<>();
                body.put("userPhoto", user.userPhoto);

                try {
                    JSONObject jsonObject = new JSONObject(gson.toJson(body));
                    Http.put(V.Lean.HTTP_UPDATE_User + "/" + user.objectId, header_copy, null, jsonObject, listener);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
